<div class="row-fluid clearfix">
    <div class="col-md-12 column">
        <ul class="breadcrumb">
            <li>
                <a href="index.html">首页</a>
            </li>
            <li class="active">
                菜单基本信息管理
            </li>
        </ul>
    </div>
    <div class="col-md-12 column">
        <div class="col-sm-12 div-page">
            <div class="col-sm-12 div-table">
                <div id="custom-toolbar">

                </div>
                <table id="table" class="table table-hover table-condensed" data-toolbar="#custom-toolbar"
                       data-click-to-select="true"  data-single-select="true" data-height="450">
                    <thead>
                    <tr>
                        <th data-field="state" data-checkbox="true"></th>
                        <th data-field="TransName" data-halign="left" data-align="left">交易名</th>
                        <th data-field="TransCode" data-halign="left" data-align="left" data-sortable="true">交易码</th>
                        <th data-field="ChildShow" data-halign="left" data-align="left" data-sortable="true">是否原子交易</th>
                        <th data-field="TransVersion" data-halign="left" data-align="left">版本号</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
        <div class="col-sm-12 alert alert-success div-alert" id="events-result" data-es="Aquí se muestra el resultado del evento"></div>
    </div>
</div>

<script>
    $(function () {

        var btnHtml ;
        var btnRole = JSON.parse($.cookie("Role_note"));

        btnHtml = '<div class="form-inline" role="form">';
        btnHtml += (btnRole["CommonDataAdd"]) ? '<button id="btn-CommonDataAdd"  type="button" role="button" class="subFun btn btn-sm btn-primary">' +
                '<span class="glyphicon glyphicon-plus"></span> 新增</button> ' : '' ;
        btnHtml += (btnRole["CommonDataModify"]) ? '<button id="btn-CommonDataModify" type="button" role="button" class="subFun btn btn-sm btn-Data btn-success disabled">' +
                '<span class="glyphicon glyphicon-pencil"></span> 修改</button> ' : '' ;
        btnHtml += (btnRole["CommonDataCancel"]) ? '<button id="btn-CommonDataCancel"  type="button" role="button" class="subFun btn btn-sm btn-Data btn-danger disabled" >' +
                '<span class="glyphicon glyphicon-trash"></span> 删除</button> ' : '' ;

        btnHtml += '<button id="btn-CommonDataDetail"  type="button" role="button" class="subFun btn btn-sm btn-Data btn-info disabled" >' +
                '<span class="glyphicon glyphicon-trash"></span> 详情</button> ' ;
        $("#custom-toolbar").append(btnHtml);

        $('#table').bootstrapTable({
            url: globalUrl+'api/MenuScript?page=1&rows=99',
            responseHandler:function(resp){
                return resp.data.rows;
            },
            pagination:false,
            search:true
        })
        .on('click-row.bs.table', function (e, row, $element) {
            //$("#events-result").text('Event: click-row.bs.table, data: ' + JSON.stringify(row));
            /* by lichun 切换选中颜色*/
            if ( $element.hasClass('selected') ) {
                $element.removeClass('info');
            }
            else {
                $element.parent('tbody').find(' > tr.selected ').removeClass("info");
                $element.addClass('info');
            }
        })
        .on('check.bs.table', function (e, row, $element) {
            //$("#events-result").text('Event: check.bs.table, data: ' + JSON.stringify(row));
            $(".btn-Data").removeClass("disabled");
        })
        .on('uncheck.bs.table', function (e, row, $element) {
            //$("#events-result").text('Event: uncheck.bs.table, data: ' + JSON.stringify(row));
            $(".btn-Data").addClass("disabled");
        })
    });


    //子功能按钮
    $('.subFun ').on("click",function () {

        var fileName=$(this).attr("id").substr(14);
        var url="../assets/tpls/MenuInfoList_"+fileName+".html";
        getTpls(url,function(resp){
            $("#modal-Template").html(resp);
            areaInit($("#area"));
            switch (fileName){
                case "Add":
                    $("#imgSelected").removeClass().html("请选择");
                    $('#validForm').find(' input[name="TransVersion"]').val("1");
                    checkAllCheck($('#validForm input[name="AreaMask"]'),$('#validForm input[name="AreaCheck"]'),$('#validForm input[name="AllAreaCheck"]'));
                    $('#validForm')
                            .formValidation()
                            .on('success.form.fv', function(e) {
                                e.preventDefault();
                                var $form = $(e.target);
                                var bv = $form.data('formValidation');
                                // Use Ajax to submit form data
                                $.ajax({
                                    data:$form.serializeArray(),
                                    type: "post",
                                    url: globalUrl+"api/MenuScript",
                                    dataType: "json",
                                    success: function (resp) {
                                        if(resp.result)
                                        {
                                            dataAddToTable($('#table'),$('#validForm'));
                                            $('#modal-Template').modal('hide');
                                        }
                                        else
                                        {
                                            alert(resp.msg);
                                        }
                                    },
                                    error:function(){
                                        alert("更新数据失败！");
                                    }
                                });


                            });
                    break;
                case "Modify":
                    dataBindToModal($('#validForm'),$('#table'));
                    dataFormatRadio($('#validForm'),'Child','ChildRadio');
                    dataFormatCheck($('#validForm'),'AreaMask','AreaCheck','AllAreaCheck');
                    $("#imgSelected").removeClass().addClass($("#Imgname").val()).html("");
                    $('#validForm')
                            .formValidation()
                            .on('success.form.fv', function(e) {
                                e.preventDefault();
                                var $form = $(e.target);
                                var bv = $form.data('formValidation');
                                // Use Ajax to submit form data
                                $.ajax({
                                    data:$form.serializeArray(),
                                    type: "put",
                                    url: globalUrl+"api/MenuScript",
                                    dataType: "json",
                                    success: function (resp) {
                                        if(resp.result)
                                        {
                                            //dataBindToTable($('#table'),$('#validForm'));
                                            $('#table').bootstrapTable('refresh',{url: globalUrl+'api/MenuScript?page=1&rows=99'});
                                            $('#modal-Template').modal('hide');
                                        }
                                        else
                                        {
                                            alert(resp.msg);
                                        }
                                    },
                                    error:function(){
                                        alert("更新数据失败！");
                                    }
                                });


                            });
                    break;
                case "Cancel":
                    dataBindToModal($('#validForm'),$('#table'));
                    dataFormatCheck($('#validForm'),'AreaMask','AreaCheck','AllAreaCheck');
                    $("#imgSelected").removeClass().addClass($("#Imgname").val()).html("");
                    $('#modal-btn-del').on("click",function () {
                        $.ajax({
                            type: "delete",
                            url: globalUrl+"api/MenuScript?"+$('#validForm').serialize(),
                            dataType: "json",
                            success: function (resp) {
                                if(resp.result)
                                {
                                    dataDelFromTable($('#table'));
                                    $('#modal-Template').modal('hide');
                                }
                                else
                                {
                                    alert(resp.msg);
                                }
                            },
                            error:function(){
                                alert("更新数据失败！");
                            }
                        });
                    });
                    break;
                default:
                    dataBindToModal($('#validForm'),$('#table'));
                    dataFormatRadio($('#validForm'),'Child','ChildRadio');
                    dataFormatCheck($('#validForm'),'AreaMask','AreaCheck','AllAreaCheck');
                    $("#imgSelected").removeClass().addClass($("#Imgname").val()).html("");
            }
            $('#modal-Template').modal('show');

            ///复选框按钮/////////////
            ///全选////
            $('#validForm input[name="AllAreaCheck"]').on("change",function () {
                checkAllCheck($('#validForm input[name="AreaMask"]'),$('#validForm input[name="AreaCheck"]'),$('#validForm input[name="AllAreaCheck"]'));
            });
            ///其他////
            $('#validForm input[name="AreaCheck"]').on("change",function () {
                checkOneCheck($('#validForm input[name="AreaMask"]'),$('#validForm input[name="AreaCheck"]'),$('#validForm input[name="AllAreaCheck"]'));
            });
            //单选按钮
            $('#validForm input[name="ChildRadio"]').on("change",function () {
                $('#validForm input[name="ChildShow"]').val($.trim($(this).parent().text()));
                $('#validForm input[name="Child"]').val($(this).val());
            });
            $(".modal .dropdown-menu li").click(function(){
                var value=$(this).find("span").attr("class");
                //$("#Imgname").val(value);
                $("#imgSelected").removeClass().addClass(value).html("");
            });
        });
    });

   function areaInit(obj){
       $.ajax({
           type: "get",
           url: globalUrl+"api/Site?orgid=910",
           dataType: "json",
           async:false,
           success: function (resp) {
               if(resp.result)
               {
                   var data=resp.data;
                   for(var i=0;i<data.length;i++)
                   {
                       var str='<label>'+
                               '<input type="checkbox" name="AreaCheck" value="'+data[i].id+'" /> '+ data[i].text+
                               '</label>';
                       obj.append(str);
                   }
               }
               else
               {
                   alert(resp.msg);
               }
           },
           error:function(){
               alert("获取地地区数据失败！");
           }
       });

   }



</script>